package models

import (
	"driver_srv/basic/config"
	"gorm.io/gorm"
	"time"
)

// 司机营业额表
type DriverRevenue struct {
	Id           uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;autoIncrement;not null;" json:"id"`                      // 记录ID
	DriverId     uint32         `gorm:"column:driver_id;type:int UNSIGNED;comment:司机ID;not null;" json:"driver_id"`                    // 关联司机ID
	OrderCount   int32          `gorm:"column:order_count;type:int;comment:订单数量;not null;default:0;" json:"order_count"`               // 当日/当月订单数
	TotalRevenue float64        `gorm:"column:total_revenue;type:decimal(10,2);comment:总营业额;not null;default:0;" json:"total_revenue"` // 总营业额
	Date         string         `gorm:"column:date;type:varchar(20);comment:日期（格式：YYYY-MM-DD 或 YYYY-MM）;not null;" json:"date"`        // 统计日期
	Type         int8           `gorm:"column:type;type:tinyint;comment:统计类型（1:日统计 2:月统计）;not null;" json:"type"`                      // 统计类型
	CreatedAt    time.Time      `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP;" json:"created_at"`
	UpdatedAt    time.Time      `gorm:"column:updated_at;type:datetime;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;" json:"updated_at"`
	DeletedAt    gorm.DeletedAt `gorm:"column:deleted_at;type:datetime;default:NULL;" json:"deleted_at"`
}

func (d *DriverRevenue) TableName() string {
	return "driver_revenue"
}

// 根据司机ID和日期查询营业额
func (d *DriverRevenue) FindByDriverAndDate() error {
	return config.DB.Where("driver_id = ? AND date = ? AND type = ?", d.DriverId, d.Date, d.Type).First(d).Error
}

// 查询司机指定时间范围的营业额列表
func (d *DriverRevenue) FindListByDriverAndTimeRange(driverId uint32, startTime, endTime string, revenueType int8) ([]DriverRevenue, error) {
	var list []DriverRevenue
	err := config.DB.Where("driver_id = ? AND date BETWEEN ? AND ? AND type = ?", driverId, startTime, endTime, revenueType).Order("date DESC").Find(&list).Error
	return list, err
}
